﻿using System.Net;
using System.Collections.Generic;
using System;

namespace 天离安全_天擎.Tl_SerVices
{
    /// <summary>
    /// 检查域名信息
    /// </summary>
    public class CheckDomainInfo_Services
    {
        /// <summary>
        /// 获取域名的IP地址
        /// </summary>
        /// <returns></returns>
        public async Task<List<string>> CheckDomainPort()
        {
            string url = Tl_Api.CheckDomainInfo_Api.Url;
            List<string> ipAddresses = new List<string>();

            try
            {
                // 从 URL 中获取主机名
                var uri = new Uri(url);
                string host = uri.Host;

                // 获取与主机名相关的 IP 地址
                var ipList = await Dns.GetHostAddressesAsync(host);

                if (ipList.Length > 0)
                {
                    string firstIp = ipList[0].ToString();
                    // 假设如果返回多个 IP 地址，则可能是 CDN
                    if (ipList.Length > 2)
                    {
                        for (int i = 1; i < ipList.Length; i++)
                        {
                            ipAddresses.Add(ipList[i].ToString());
                        }
                        string ipAddressesString = string.Join("\r\n", ipAddresses);
                        // CDNIP地址
                        Tl_Api.CheckDomainInfo_Api.Cip.Append(ipAddressesString);
                    }
                    else
                    {
                        // 真实IP地址
                        Tl_Api.CheckDomainInfo_Api.Ip = firstIp;
                    }
                }
                else
                {
                    ipAddresses.Add("没有找到 IP 地址");
                }
            }
            catch (Exception ex)
            {
                Tl_Api.CheckDomainInfo_Api.Error.Append($"发生错误: {ex.Message}");
            }

            return ipAddresses;
        }
    }
}
